const { releasePet, removeRelease } = require("../model/releaseModel")
const { sysTime } = require("../utils/getNowTime")
module.exports = {
  // 发布宠物
  releasePet: async (ctx) => {
    for (var key in ctx.request.body) {
      if (!ctx.request.body[key]) {
        ctx.body = {
          status: 205,
          msg: "缺少参数"
        }
        return;
      };
    }
    // 结构请求对象
    const { user_id, category_id, pet_name, pet_variety, pet_gender, pet_age, pet_vaccin, pet_sterilization, pet_Insect_repellent, pet_giveaddress, pet_images, pet_intro } = ctx.request.body;
    // 宠物信息对象
    const petInformation = {
      user_id, // 用户id
      category_id, // 宠物类别id
      pet_name, // 宠物名称
      pet_variety, // 宠物品种
      pet_gender, // 宠物性别
      pet_age, // 宠物年龄
      pet_vaccin, // 宠物疫苗状态
      pet_sterilization, // 宠物绝育状态
      pet_Insect_repellent, // 宠物驱虫状态
      pet_giveaddress, // 宠物送养地址
      pet_images, // 宠物图片
      pet_intro, // 宠物介绍
      pet_createtime: sysTime() // 宠物发布时间
    }
    // 插入数据
    let res = await releasePet(petInformation);
    if (res.affectedRows === 1) {
      ctx.body = {
        status: 200,
        msg: "发布成功"
      }
    } else {
      ctx.body = {
        status: 205,
        msg: "发布失败"
      }
    }
  },
  // 删除发布
  removeRelease: async (ctx) => {
    const { pet_id } = ctx.request.body;
    // 删除发布
    let res = await removeRelease(pet_id);

    if (res.affectedRows === 1) {
      ctx.body = {
        status: 200,
        msg: "删除成功"
      }
    } else {
      ctx.body = {
        status: 205,
        msg: "删除失败"
      }
    }
  }
}